//
//  ViewController.m
//  阿里星球-音乐界面
//
//  Created by Mars_ZXB on 16/8/13.
//  Copyright © 2016年 BAT3. All rights reserved.
//

#import "ViewController.h"
#import "ALMusicCollectionCell.h"
#import "ALHeadShufCollectionCell.h"
#import "ALMusicRecommendCollectionCell.h"
#import "ALExclusiveCollectionCell.h"
#import "ALBodyCollectionViewCell.h"
#import "ALMusicCollectionReusable.h"
#import "AFNetworking.h"
#import "ALResult.h"
#import "ALResultData.h"
#import "ALContentData.h"

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray<ALResultData *> *resultDataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation];
    _collectionView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
    
    [_collectionView registerClass:[ALMusicCollectionReusable class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    
    [self handleData];
}

- (void)setNavigation
{
    UIImage *image = [UIImage imageNamed:@"barBackgroundImage"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

    _segment.layer.masksToBounds = YES;
    _segment.layer.cornerRadius = CGRectGetHeight(_segment.frame) / 2;
    _segment.layer.borderWidth = 1;
    _segment.layer.borderColor = [UIColor whiteColor].CGColor;
    
}


- (void)handleData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        ALResult *resultModel = [ALResult modelWithDictionary:result];
        _resultDataArr = resultModel.data.mutableCopy;
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _resultDataArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _resultDataArr[section].data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ALHeadShufCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"alHeadShufCollectionCell" forIndexPath:indexPath];
        NSMutableArray *images = [NSMutableArray array];
        for (ALContentData *model in _resultDataArr[0].data) {
            [images addObject:model.picUrl];
        }
        cell.images = images;
        return cell;
    } else if (indexPath.section == 1) {
        ALMusicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"alMusicCollectionCell" forIndexPath:indexPath];
        cell.model = _resultDataArr[indexPath.section].data[indexPath.item];
        return cell;
    } else if (indexPath.section == 2) {
        ALMusicRecommendCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"alMusicRecommendCollectionCell" forIndexPath:indexPath];
        cell.model = _resultDataArr[indexPath.section].data[indexPath.item];
        return cell;
    } else if (_resultDataArr[indexPath.section].style == 5) {
        ALBodyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"alBodyCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.section == 3 || indexPath.section == 5) {
            cell.backgroundColor = [UIColor colorWithRed:0.04 green:0.06 blue:0.07 alpha:1];
        }
        cell.model = _resultDataArr[indexPath.section].data[indexPath.item];
        return cell;
    } else if (_resultDataArr[indexPath.section].style == 2) {
        ALExclusiveCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"alExclusiveCollectionCell" forIndexPath:indexPath];
        cell.model = _resultDataArr[indexPath.section].data[indexPath.item];
        return cell;
    }
    return nil;
}

#pragma mark - flow layout delegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = 0.0f;
    CGFloat height = 0.0f;
    if (indexPath.section == 0) {
        width = CGRectGetWidth(self.view.frame) - 5;
        height = width / 2;
    } else if (indexPath.section == 1) {
        width = CGRectGetWidth(self.view.frame) / 3 - 0.5;
        height = width / 2;
    } else if (indexPath.section == 2) {
        width = (CGRectGetWidth(self.view.frame) - 25) / 2;
        height = width / 2;
    } else if (_resultDataArr[indexPath.section].isNameDisplay == 1 && _resultDataArr[indexPath.section].style == 5) {
        width = (CGRectGetWidth(self.view.frame) - 30) / 3;
        height = width + 50;
    } else if (_resultDataArr[indexPath.section].isNameDisplay == 1 && _resultDataArr[indexPath.section].style == 2) {
        width = CGRectGetWidth(self.view.frame) - 20;
        height = width / 3 / 1.8 + 10;
    }
    return CGSizeMake(width, height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 10);
    } else if (section == 1) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return UIEdgeInsetsMake(0, 10, 10, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return CGSizeZero;
    } else {
        return CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) / 13);
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        ALMusicCollectionReusable *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        header.nameLabel.text = _resultDataArr[indexPath.section].name;
        if (indexPath.section == 2) {
            header.moreBtn.hidden = YES;
        } else {
            header.moreBtn.hidden = NO;
        }
        
        return header;
    }
    return nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
